# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=docbook-xsl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.79.2
pkgrel=7
pkgdesc='XML stylesheets for Docbook-xml transformations (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('custom')
url='https://docbook.org/'
depends=("${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-docbook-xml")
install=${_realname}-${MSYSTEM}.install
source=(https://github.com/docbook/xslt10-stylesheets/releases/download/release%2F${pkgver}/docbook-xsl{,-nons}-${pkgver}.tar.gz
        "0001-add-back-old-uri-rewrite.patch"
        "0002-fix-infinity.patch")
sha256sums=('966188d7c05fc76eaca115a55893e643dd01a3486f6368733c9ad974fcee7a26'
            'f89425b44e48aad24319a2f0d38e0cb6059fdc7dbaf31787c8346c748175ca8e'
            '1aef3b01c69cce3552614033cdee9d26e4d6b3770cdf7d62b4055f9062adb9c0'
            '4a27dd468bb803290b73aeb3a0fde190a1897b97945b59eaf39d8535bb6332a5')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  msg2 "${_realname}-${pkgver}"
  apply_patch_with_msg 0001-add-back-old-uri-rewrite.patch \
    0002-fix-infinity.patch

  cd "${srcdir}"/${_realname}-nons-${pkgver}

  msg2 "${_realname}-nons-${pkgver}"
  apply_patch_with_msg \
    0002-fix-infinity.patch
}

package() {
  cd ${srcdir}
  mkdir -p "$pkgdir${MINGW_PREFIX}/etc/xml"
  ${MINGW_PREFIX}/bin/xmlcatalog --noout --create "${pkgdir}${MINGW_PREFIX}/etc/xml/docbook-xsl"

  local pkgroot ns dir
  for ns in -nons ''; do
    pkgroot="$pkgdir${MINGW_PREFIX}/share/xml/docbook/xsl-stylesheets$ns-$pkgver"
    dir=${_realname}$ns-$pkgver
    install -Dt ${pkgroot} -m644 $dir/catalog.xml
    install -Dt "$pkgroot" -m644 $dir/VERSION{,.xsl}

    (
      shopt -s nullglob  # ignore missing files
      for fn in assembly common eclipse epub epub3 fo highlighting html \
        htmlhelp javahelp lib manpages params profiling roundtrip template \
        website xhtml xhtml-1_1 xhtml5
      do
        install -Dt "$pkgroot/$fn" -m644 $dir/$fn/*.{xml,xsl,dtd,ent}
      done
    )
  done

  # sf nons version
  ${MINGW_PREFIX}/bin/xmlcatalog --noout --add delegateURI \
      "http://docbook.sourceforge.net/release/xsl/" \
      "../../share/xml/docbook/xsl-stylesheets-$pkgver/catalog.xml" \
      "${pkgdir}${MINGW_PREFIX}/etc/xml/docbook-xsl"
  ${MINGW_PREFIX}/bin/xmlcatalog --noout --add delegateSystem \
      "http://docbook.sourceforge.net/release/xsl/" \
      "../../share/xml/docbook/xsl-stylesheets-$pkgver/catalog.xml" \
      "${pkgdir}${MINGW_PREFIX}/etc/xml/docbook-xsl"

  # sf ns version
  ${MINGW_PREFIX}/bin/xmlcatalog --noout --add delegateURI \
      "http://docbook.sourceforge.net/release/xsl-ns/" \
      "../../share/xml/docbook/xsl-stylesheets-$pkgver/catalog.xml" \
      "${pkgdir}${MINGW_PREFIX}/etc/xml/docbook-xsl"
  ${MINGW_PREFIX}/bin/xmlcatalog --noout --add delegateSystem \
      "http://docbook.sourceforge.net/release/xsl-ns/" \
      "../../share/xml/docbook/xsl-stylesheets-$pkgver/catalog.xml" \
      "${pkgdir}${MINGW_PREFIX}/etc/xml/docbook-xsl"

  # cdn ns version
  ${MINGW_PREFIX}/bin/xmlcatalog --noout --add delegateURI \
      "http://cdn.docbook.org/release/xsl/" \
      "../../share/xml/docbook/xsl-stylesheets-$pkgver/catalog.xml" \
      "${pkgdir}${MINGW_PREFIX}/etc/xml/docbook-xsl"
  ${MINGW_PREFIX}/bin/xmlcatalog --noout --add delegateSystem \
      "http://cdn.docbook.org/release/xsl/" \
      "../../share/xml/docbook/xsl-stylesheets-$pkgver/catalog.xml" \
      "${pkgdir}${MINGW_PREFIX}/etc/xml/docbook-xsl"

  # cdn nons version
  ${MINGW_PREFIX}/bin/xmlcatalog --noout --add delegateURI \
      "http://cdn.docbook.org/release/xsl-nons/" \
      "../../share/xml/docbook/xsl-stylesheets-nons-$pkgver/catalog.xml" \
      "${pkgdir}${MINGW_PREFIX}/etc/xml/docbook-xsl"
  ${MINGW_PREFIX}/bin/xmlcatalog --noout --add delegateSystem \
      "http://cdn.docbook.org/release/xsl-nons/" \
      "../../share/xml/docbook/xsl-stylesheets-nons-$pkgver/catalog.xml" \
      "${pkgdir}${MINGW_PREFIX}/etc/xml/docbook-xsl"

  install -Dt "$pkgdir${MINGW_PREFIX}/share/licenses/$pkgname" -m644 $dir/COPYING
}
